﻿using B.Z.BasicData.Infrastructure;
using B.Z.Inventory.API.Read.Application.Command;
using B.Z.Inventory.API.Read.DTO;
using B.Z.Inventory.Domain;
using B.Z.Inventory.ErrorCode;
using MediatR;
using Microsoft.EntityFrameworkCore;

namespace B.Z.Inventory.API.Read.Application.CommandHandler
{
    public class InventoryDtoQueryCommandHandler : IRequestHandler<InventoryDtoQueryCommand, APIResult<APIPaging<InventoryDto>>>
    {
        private readonly IBaseRepository<T_InventoryTask> inventoryTaskRepository;
        private readonly IBaseRepository<T_Store> storeRepository;
        private readonly IBaseRepository<T_InventoryGoods> inventorygoodsRepository;
        private readonly ILogger<InventoryDtoQueryCommandHandler> logger;
        public InventoryDtoQueryCommandHandler(IBaseRepository<T_InventoryTask> inventoryTaskRepository, ILogger<InventoryDtoQueryCommandHandler> logger, IBaseRepository<T_Store> storeRepository, IBaseRepository<T_InventoryGoods> inventorygoodsRepository)
        {
            this.inventoryTaskRepository = inventoryTaskRepository;
            this.logger = logger;
            this.storeRepository = storeRepository;
            this.inventorygoodsRepository = inventorygoodsRepository;
        }

        public Task<APIResult<APIPaging<InventoryDto>>> Handle(InventoryDtoQueryCommand request, CancellationToken cancellationToken)
        {
            APIResult<APIPaging<InventoryDto>> result = new APIResult<APIPaging<InventoryDto>>();
            result.Code = APIEnums.ok;
            var list = from a in inventoryTaskRepository.GetAll().AsNoTracking().Where(x=>x.AttnId==request.UserId)
                       join b in storeRepository.GetAll().AsNoTracking()
                       on a.StoreId equals b.Id
                       select new InventoryDto
                       {
                           Id = a.Id,
                           InventoryNnumber = a.InventoryNnumber,
                           InventoryStatus = a.InventoryStatus,
                           Mid = a.Mid,
                           InventoryType = a.InventoryType,
                           InventoryRange = a.InventoryRange,
                           InventoryStartTime = a.InventoryStartTime,
                           InventoryEndTime = a.InventoryEndTime,
                           Note = a.Note,
                           TaskType = a.TaskType,
                           AttnId = a.AttnId,
                           AttnName = a.AttnName,
                           CreateUserName = a.CreateUserName,
                           StoreName = b.StoreName,
                           IsHaveGoods=a.IsHaveGoods,
                       };

            if (request.InventoryStatus != null) 
            {
                list = list.Where(x => x.InventoryStatus == request.InventoryStatus);
            }
            APIPaging<InventoryDto> paging = new APIPaging<InventoryDto>();
            paging.Count = list.Count();
            paging.List = list.OrderByDescending(x => x.Id).Skip((request.PageIndex - 1) * request.PageSize).Take(request.PageSize).ToList();
            result.Data = paging;
            return Task.FromResult(result);
        }
    }
}
